import { Column, Entity, ManyToOne, Index } from 'typeorm';
import { BaseEntity } from '../../../common/entities/base.entity';
import { User } from '../../user/entities/user.entity';
import { Novel } from './novel.entity';
import { Bookshelf } from './bookshelf.entity';

@Entity('user_favorites')
@Index(['userId'], { unique: false })
@Index(['novelId'], { unique: false })
@Index(['userId', 'novelId'], { unique: true })
export class UserFavorite extends BaseEntity {
  @Column({ type: 'bigint', comment: '用户ID' })
  userId: number;

  @Column({ type: 'bigint', comment: '小说ID' })
  novelId: number;

  @Column({ type: 'bigint', nullable: true, comment: '书架ID，默认为null表示默认书架' })
  bookshelfId?: number | null;

  // 关联关系
  @ManyToOne(() => User, (user) => user.favorites, { onDelete: 'CASCADE' })
  user: User;

  @ManyToOne(() => Novel, (novel) => novel.favorites, { onDelete: 'CASCADE' })
  novel: Novel;

  @ManyToOne(() => Bookshelf, (bookshelf) => bookshelf.favorites, { onDelete: 'SET NULL' })
  bookshelf: Bookshelf;
}
